<template>
  <BasicModal v-bind="$attrs" @register="registerModal" destroyOnClose :title="title" :width="1024" @ok="handleSubmit">
    <BasicForm @register="registerForm" ref="formRef">
      <template #timeSlot="{ model, field }">
        <a-input-search :disabled="isDetail" v-model:value="model[field]" placeholder="请选择时格" @search="openTime" />
      </template>
    </BasicForm>
    <!-- 子表单区域 -->
    <a-tabs v-model:activeKey="activeKey" animated @change="handleChangeTabs">
      <a-tab-pane tab="排产优先级" key="psAlwaysSchedulingSchemeItem" :forceRender="true">
        <JVxeTable
          keep-source
          resizable
          ref="psAlwaysSchedulingSchemeItem"
          :loading="psAlwaysSchedulingSchemeItemTable.loading"
          :columns="psAlwaysSchedulingSchemeItemTable.columns"
          :dataSource="psAlwaysSchedulingSchemeItemTable.dataSource"
          :height="340"
          :serialNumber="true"
          :rowRadio="false"
          :disabled="formDisabled"
          :toolbar="true"
        />
      </a-tab-pane>
      <a-tab-pane tab="分厂规则" key="psAlwaysSchedulingSchemeItem2" :forceRender="true">
        <JVxeTable
          keep-source
          resizable
          ref="psAlwaysSchedulingSchemeItem2"
          :loading="psAlwaysSchedulingSchemeItemTable2.loading"
          :columns="psAlwaysSchedulingSchemeItemTable2.columns"
          :dataSource="psAlwaysSchedulingSchemeItemTable2.dataSource"
          :height="340"
          :rowNumber="true"
          :rowSelection="true"
          :disabled="formDisabled"
          :toolbar="true"
        />
      </a-tab-pane>
      <a-tab-pane tab="分线规则" key="psAlwaysSchedulingSchemeItem3" :forceRender="true">
        <JVxeTable
          keep-source
          resizable
          ref="psAlwaysSchedulingSchemeItem3"
          :loading="psAlwaysSchedulingSchemeItemTable3.loading"
          :columns="psAlwaysSchedulingSchemeItemTable3.columns"
          :dataSource="psAlwaysSchedulingSchemeItemTable3.dataSource"
          :height="340"
          :rowNumber="true"
          :rowSelection="true"
          :disabled="formDisabled"
          :toolbar="true"
        />
      </a-tab-pane>
    </a-tabs>
  </BasicModal>
  <!-- 时格 -->
  <timeSelectModel @register="timeRegModal" @getSelectResult="timehandleSuccess" v-bind="getBindValue"></timeSelectModel>
</template>

<script lang="ts" setup>
import { ref, computed, unref, reactive } from 'vue';
import { BasicModal, useModalInner, useModal } from '/@/components/Modal';
import { BasicForm, useForm } from '/@/components/Form/index';
import { JVxeTable } from '/@/components/jeecg/JVxeTable';
import { useJvxeMethod } from '/@/hooks/system/useJvxeMethods.ts';
import {
  formSchema,
  psAlwaysSchedulingSchemeItemColumns,
  psAlwaysSchedulingSchemeItemColumns2,
  psAlwaysSchedulingSchemeItemColumns3,
} from '../PsAlwaysSchedulingScheme.data';
import { saveOrUpdate, psAlwaysSchedulingSchemeItemList } from '../PsAlwaysSchedulingScheme.api';
import { VALIDATE_FAILED } from '/@/utils/common/vxeUtils';
import timeSelectModel from '/@/views/components/timeSelectModel.vue';

// Emits声明
const emit = defineEmits(['register', 'success']);
const isUpdate = ref(true);
const isDetail = ref<boolean>(false)
const formDisabled = ref(false);
const refKeys = ref(['psAlwaysSchedulingSchemeItem', 'psAlwaysSchedulingSchemeItem2', 'psAlwaysSchedulingSchemeItem3']);
const activeKey = ref('psAlwaysSchedulingSchemeItem');
const psAlwaysSchedulingSchemeItem = ref();
const psAlwaysSchedulingSchemeItem2 = ref();
const psAlwaysSchedulingSchemeItem3 = ref();
const tableRefs = { psAlwaysSchedulingSchemeItem, psAlwaysSchedulingSchemeItem2, psAlwaysSchedulingSchemeItem3 };
const psAlwaysSchedulingSchemeItemTable = reactive({
  loading: false,
  dataSource: [],
  columns: psAlwaysSchedulingSchemeItemColumns,
});
const psAlwaysSchedulingSchemeItemTable2 = reactive({
  loading: false,
  dataSource: [],
  columns: psAlwaysSchedulingSchemeItemColumns2,
});
const psAlwaysSchedulingSchemeItemTable3 = reactive({
  loading: false,
  dataSource: [],
  columns: psAlwaysSchedulingSchemeItemColumns3,
});
//表单配置
const [registerForm, { setProps, resetFields, setFieldsValue, validate }] = useForm({
  labelWidth: 100,
  schemas: formSchema,
  showActionButtonGroup: false,
  baseColProps: { span: 8 },
});
//表单赋值
const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
  //重置表单
  await reset();
  setModalProps({ confirmLoading: false, showCancelBtn: data?.showFooter, showOkBtn: data?.showFooter });
  isUpdate.value = !!data?.isUpdate;
  isDetail.value = !!data?.isDetail
  formDisabled.value = !data?.showFooter;
  if (unref(isUpdate)) {
    //表单赋值
    await setFieldsValue({
      ...data.record,
    });
  await requestSubTableData(psAlwaysSchedulingSchemeItemList, { id: data?.record?.id }, psAlwaysSchedulingSchemeItemTable);
  }
  // 隐藏底部时禁用整个表单
  setProps({ disabled: !data?.showFooter });
});
//方法配置
const [handleChangeTabs, handleSubmit, requestSubTableData, formRef] = useJvxeMethod(
  requestAddOrEdit,
  classifyIntoFormData,
  tableRefs,
  activeKey,
  refKeys
);

//设置标题
const title = computed(() => ( unref(isDetail) ? '详情' : !unref(isUpdate) ? '新增' : '编辑'));

async function reset() {
  await resetFields();
  activeKey.value = 'psAlwaysSchedulingSchemeItem';
  psAlwaysSchedulingSchemeItemTable.dataSource = [];
  psAlwaysSchedulingSchemeItemTable2.dataSource = [];
  psAlwaysSchedulingSchemeItemTable3.dataSource = [];
}
function classifyIntoFormData(allValues) {
  let main = Object.assign({}, allValues.formValue);
  return {
    ...main, // 展开
    psAlwaysSchedulingSchemeItemList: allValues.tablesValue[0].tableData,
  };
}
//表单提交事件
async function requestAddOrEdit(values) {
  values.psAlwaysSchedulingSchemeItemList = values.psAlwaysSchedulingSchemeItemList.concat(
    psAlwaysSchedulingSchemeItem2.value!.getTableData(),
    psAlwaysSchedulingSchemeItem3.value!.getTableData()
  );
  try {
    setModalProps({ confirmLoading: true });
    //提交表单
    await saveOrUpdate(values, isUpdate.value);
    //关闭弹窗
    closeModal();
    //刷新列表
    emit('success');
  } finally {
    setModalProps({ confirmLoading: false });
  }
}
const [timeRegModal, { openModal: openTimeModal }] = useModal();
const getBindValue = Object.assign({}, '');

function openTime() {
  openTimeModal(true, {
    isUpdate: false,
    showFooter: true,
  });
}
function timehandleSuccess(val, values) {
  setFieldsValue({
    timeFormatName: val[0].label,
    timeFormatId: val[0].value,
  });
}
</script>

<style lang="less" scoped>
/** 时间和数字输入框样式 */
:deep(.ant-input-number) {
  width: 100%;
}

:deep(.ant-calendar-picker) {
  width: 100%;
}
</style>
